package com.study.example.gameserver.module.gm.controller;

import com.sparrow.common.util.StringUtil;
import com.sparrow.event.annotation.EventReceiver;
import com.sparrow.example_message.gm.req.ReqUseGMPacket;
import com.sparrow.example_message.gm.resp.RespUseGMPacket;
import com.sparrow.socket.tcp.anno.PacketHandler;
import com.study.example.gameserver.log.SystemLogType;
import com.study.example.gameserver.module.backpack.service.BackPackService;
import com.study.example.gameserver.module.buff.service.BuffService;
import com.study.example.gameserver.module.gm.anno.GM;
import com.study.example.gameserver.module.gm.manager.GMManager;
import com.study.example.gameserver.module.scene.domain.SceneMap;
import com.study.example.gameserver.module.scene.service.SceneService;
import com.study.example.gameserver.module.player.entity.Player;
import com.study.example.gameserver.module.player.service.PlayerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 管理员命令
 */
@PacketHandler
@Component
public class GMController {

    private static Logger logger = LoggerFactory.getLogger(GMController.class);
    @Autowired
    GMManager gmManager;
    @Autowired
    BackPackService backPackService;
    @Autowired
    PlayerService playerService;
    @Autowired
    BuffService buffService;
    @Autowired
    SceneService sceneService;

    @EventReceiver
    public void reqUseGM(Player player, ReqUseGMPacket req) {
        if (StringUtil.isEmpty(req.getCommand())) {
            return;
        }
        RespUseGMPacket resp = new RespUseGMPacket();
        try {
            Object result = gmManager.useCommand(player, req.getCommand());
            if (result == null) {
                resp.setResult("成功");
            } else {
                resp.setResult(result.toString());
            }
        } catch (Exception e) {
            resp.setError(e.getMessage());
            throw e;
        }
        player.sendPacket(resp);
    }

    @GM(cmd =  "scene", desc = "切换场景")
    public String changeScene(Player player, int sceneId) {
        SceneMap sceneMap = sceneService.getMap(sceneId, 1);
        sceneService.changeMap(player, sceneMap, () -> {

        });
        return "切换场景";
    }

    @GM(cmd = "goods", desc = "添加道具")
    public String goods(Player player, int goodsId, int num) {
        if (!backPackService.addGoods(player, goodsId, num)) {
            return "背包已满";
        }
        logger.info("玩家{} 添加道具{} * {}", player.getPlayerName(), goodsId, num);
        return "添加道具成功";
    }

    @GM(cmd = "deduct", desc = "扣减道具")
    public void deduct(Player player, int goodsId, int num) {
        backPackService.deductGoods(player, goodsId, num);
        logger.info("玩家{} 扣减道具{} * {}", player.getPlayerName(), goodsId, num);
    }

    @GM(cmd = "addExp", desc = "增加经验")
    public void addExp(Player player, int addExp) {
        playerService.addExp(player, addExp);
        logger.info("玩家{} addExp {}", player.getPlayerName(), addExp);
    }

    @GM(cmd = "addHp", desc = "addHp")
    public void addHp(Player player, int addVal) {
        player.setHp(player.getHp() + addVal);
        if (player.getHp() > player.getMaxHp()) {
            player.setHp(player.getMaxHp());
        }
        player.save();

        playerService.sendHpInfo(player);
    }

    @GM(cmd = "addBuff", desc = "添加buff")
    public void addBuff(Player player, int buffId) {
        buffService.addBuff(player, buffId);
    }

    @GM(cmd = "hello", desc = "hello")
    public void hello(Player player) {
        System.out.println("hello " + player.getPlayerName());
    }

    @GM(cmd = "testError", desc = "testError")
    public void testError(Player player) {
        int x = 10 / 0;
        SystemLogType.system.info("x = {} ", x);
    }

}
